package com.woniuxy.controller;


import com.woniuxy.common.Result;
import com.woniuxy.config.WebMvcConfig;
import com.woniuxy.controller.form.OrderOutForm;
import com.woniuxy.controller.form.OrderReviewForm;
import com.woniuxy.service.DietOrderService;
import com.woniuxy.service.dto.AllOrderDto;
import com.woniuxy.service.dto.StoreOrderInfoDto;
import com.woniuxy.service.dto.WaiteOrderDto;
import com.woniuxy.util.RedisUtil;
import com.woniuxy.util.WebUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 外卖订单 前端控制器
 * </p>
 *
 * @author zz
 * @since 2023年06月13日
 */
@RestController
@RequestMapping("/dietOrder")
@Api(tags = "订单相关业务")
public class DietOrderController {

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private DietOrderService dietOrderService;

    @ApiOperation("展示当前商家待处理订单接口")
    @GetMapping ("/showStoreOrder")
    public Result showStoreOrder(@RequestHeader String accessToken) throws Exception{
        Integer accountId = redisUtil.getUserIdByToken(accessToken);
        StoreOrderInfoDto dto = dietOrderService.showStoreOrder(accountId.longValue());
        return Result.success(200,"展示当前商家待接订单成功",dto);
    }

    @ApiOperation("展示当前商家待接受订单接口")
    @GetMapping ("/showWaiteOrder")
    public Result showWaiteOrder(@RequestHeader String accessToken) throws Exception{
        Integer accountId = redisUtil.getUserIdByToken(accessToken);
        List<WaiteOrderDto> list = dietOrderService.showWaiteOrder(accountId.longValue());
        return Result.success(200,"展示当前商家待接订单成功",list);
    }


    @ApiOperation("商家接单/拒单接口")
    @PostMapping("/orderReview")
    public Result orderReview( @RequestBody OrderReviewForm orderReviewForm ) throws Exception{
        String accessToken = WebUtil.getRequest().getHeader("accessToken");
        Integer accountId = redisUtil.getUserIdByToken(accessToken);
        dietOrderService.orderReview(orderReviewForm,accountId.longValue());
        return Result.success(200,"操作成功",null);
    }

    @ApiOperation("商家出餐接口")
    @PostMapping("/orderOut")
    public Result orderOut(@RequestBody OrderOutForm form , @RequestHeader String accessToken) throws Exception{
        Integer accountId = redisUtil.getUserIdByToken(accessToken);
        System.out.println(form.getOrderNum());
        StoreOrderInfoDto dto = dietOrderService.orderOut(form.getOrderNum(),accountId.longValue());
        return Result.success(200,"出餐成功",dto);
    }

    @ApiOperation("展示当前商家所有订单接口")
    @GetMapping ("/showAllOrder")
    public Result showAllOrder(@RequestHeader String accessToken) throws Exception{
        Integer accountId = redisUtil.getUserIdByToken(accessToken);
        List<AllOrderDto> list = dietOrderService.showAllOrder(accountId.longValue());
        return Result.success(200,"展示当前商家所有订单成功",list);
    }




}

